#pragma once

#include "Triangle.h"

struct SphereTreeIndex
{
public:
	SphereTreeIndex(int iLevel_, DWORD dwIndex_);

	int iLevel;
	DWORD dwIndex;
};

class CSphereTreeNode
{
public:
	CSphereTreeNode(int iLevel);
	virtual ~CSphereTreeNode();

	void BuildTree(const std::vector<VectorR>& vVertices, const std::vector<CTriangle>& vTriangles, const std::vector<int> vTriangleIndices, int iMaxLevel, DWORD dwIndex = 0);
	void Render(int iLevel, const D3DXVECTOR3& vTranslate = D3DXVECTOR3(0.f, 0.f, 0.f));
	const CSphereTreeNode* GetNode(const SphereTreeIndex& index) const;

	void InitD3DBuffer();
	void Destroy();

	int CountNodes(int iLevel) const;

	void Translate(const D3DXVECTOR3& vTranslate);

	const D3DXVECTOR3& GetCenter() const { return m_vSphereCenter; }
	float GetSphereRadius() const { return m_fSphereRadius; }

private:
	void DoRender(const D3DXVECTOR3& vTranslate = D3DXVECTOR3(0.f, 0.f, 0.f));
	std::vector<int> m_vTriangleIndices;

	CSphereTreeNode* m_pLeft;
	CSphereTreeNode* m_pRight;

	int m_iLevel; 
	DWORD m_dwIndex;

	D3DXVECTOR3 m_vSphereCenter;
	float m_fSphereRadius;
	LPD3DXMESH m_pSphereMesh;
};

class CSphereTree
{
public:
	CSphereTree(void);
	virtual ~CSphereTree(void);

	void BuildTree(const std::vector<VectorR>& vVertices, const std::vector<CTriangle>& vTriangles, int iMaxLevel);
	CSphereTreeNode* GetRootNode() { return m_pRoot; }

	const CSphereTreeNode* GetNode(const SphereTreeIndex& index) const;

	void Render(int iLevel, const D3DXVECTOR3& vTranslate = D3DXVECTOR3(0.f, 0.f, 0.f));
	void InitD3DBuffer();
	void Destroy();

	int CountNodes(int iLevel) const;

	void Translate(const D3DXVECTOR3& vTranslate);

private:
	CSphereTreeNode* m_pRoot;
};

